<?php
/**
 * 节点人关系图管理
 *
 *
 *
 *
 * @网店运维提供技术支持 授权请购买shopnc授权
 * @license    http://www.shopwwi.com
 * @link       交流群号：111731672
 */



defined('ByShopWWI') or exit('Access Invalid!');

class nodeControl extends SystemControl{
   
    private static $downline_member_fee=0;//某会员的所有下线会员费总和
    private static $downline_members=array();//某会员的所有下线会员ID
   
    private static $member_array_list=[];
    public function __construct(){
        parent::__construct();
       
    }

    public function indexOp() {
        $this->nodeOp();
    }

    
    public function nodeOp(){
        
        $node_id=!empty($_GET['node_id'])?(int)$_GET['node_id']:0;
            
        $this->getAllNodes($node_id);      
        $result= json_encode(self::$member_array_list);
        
        Tpl::output('result', $result);
	Tpl::setDirquna('shop');
        Tpl::showpage('node.index');
    }
    
      
    public function getAllNodes($node_id)
    {
        $currentLevel=0;
        self::$member_array_list=[];
        $model_member=Model('member');
        $member= $model_member->where(array('member_id'=>$node_id))->find();
        if(!empty($member))
        {
           self::$member_array_list[0]['key']=$member['member_id'];
           self::$member_array_list[0]['parent']=$member['node_id'];
           self::$member_array_list[0]['member_name']=$member['member_name'].'('.$member['member_truename'].')(ID:'.$member['member_id'].')';
           self::$member_array_list[0]['is_active']=$member['member_state'];
           self::$member_array_list[0]['member_type']=$model_member->getMemberTypeName($member['member_type']);
           self::$downline_members=[];self::$downline_member_fee=0;
           $this->getDownLineMembersAndFee($member['member_id']);
           self::$member_array_list[0]['team_count']= count(self::$downline_members);
           self::$member_array_list[0]['profit']=self::$downline_member_fee;
           self::$member_array_list[0]['floor']='第【1】层';
           $currentLevel++;

        }
        $this->getChildNodes($node_id,$currentLevel);
    }


    public function getChildNodes($node_id,$currentLevel=0)
    {

        $model_member=Model('member');
        $member_array=array();
        $member_array_list=array();
        $member_list= $model_member->where(array('node_id'=>$node_id))->select();
        foreach($member_list as $member)
        {
           $member_array['key']=$member['member_id'];
           $member_array['parent']=$member['node_id'];
           $member_array['member_name']=$member['member_name'].'('.$member['member_truename'].')(ID:'.$member['member_id'].')';
           $member_array['is_active']=$member['member_state'];
           $member_array['member_type']=$model_member->getMemberTypeName($member['member_type']);
           self::$downline_members=[];self::$downline_member_fee=0;
           $this->getDownLineMembersAndFee($member['member_id']);
           $member_array['team_count']= count(self::$downline_members);
           $member_array['profit']=self::$downline_member_fee;
           $member_array['floor']='第【'.($currentLevel+1).'】层';
           self::$member_array_list[]=$member_array;

           $this->getChildNodes($member['member_id'],++$currentLevel);
           --$currentLevel;
        }
    }

    /**
     * 获取某会员的所有下线会员和此会员的总业绩
     * @param type $member_id
     */
    public function getDownLineMembersAndFee($member_id)
    {
        $model_member=Model('member');
        $member_fee_total=0;
        $childNodes=$model_member->getChildNodes2($member_id);
        foreach($childNodes as $childNode)
        {   
            $member_fee=$childNode['member_fee'];
            $member_fee=$member_fee+$member_fee*$childNode['repeat_count'];
            $member_fee_total+=$member_fee;
            self::$downline_member_fee=self::$downline_member_fee+$member_fee_total;
            self::$downline_members[]=$childNode['member_id'];
            $this->getDownLineMembersAndFee($childNode['member_id']);
        }
    }

    
    

}